<?php

namespace app\api\controller\user;

use app\api\controller\AuthController;
use app\api\service\InviteService;
use app\api\service\UserService;
use think\exception\ValidateException;
use think\facade\Filesystem;

class InfoController extends AuthController
{
    /**
     * 添加用户等级
     */
    public function addUserShip()
    {
        $param = $this->request->param();
        if (empty($param['invite_code'])) {
            return json(['code' => 0, 'msg' => '参数有误']);
        }
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '参数非法']);
        }
        $code = $param['invite_code'];
        $user_id = $this->request->user['user_id'];
        $invite = new InviteService();
        $pid = $invite->code2ID($code);
        if (!$pid) {
            return json(['code' => 0, 'msg' => '参数有误']);
        }
        $userService = new UserService();
        $userService->addShip($pid, $user_id, $param['store_id']);
        return json(['code' => 1, 'msg' => '操作成功!']);
        // return $rst ? json(['code' => 1, 'msg' => '添加成功!']) : json(['code' => 0, 'msg' => '添加失败!']);
    }

    /**
     * 修改用户资料
     * */
    public function change()
    {
        $param = $this->request->param();
        // print_r($param);exit;
        try {
            $this->validate($param, [
                'avatar' => 'requireWith',
                'gender' => 'requireWith|in:0,1,2',
                'nickname' => 'requireWith',
                'birthday' => 'requireWith|dateFormat:Y-m-d',
            ], [
                'avatar.requireWith' => '头像不能为空',
                'gender.requireWith' => '性别不能为空',
                'gender.in' => '性别不合法',
                'nickname.requireWith' => '性别不能为空',
                'birthday.requireWith' => '出生日期不能为空',
            ]);
        } catch (ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }
        $userService = new UserService();
        $user_id = $this->request->user['user_id'];
        $result = $userService->changeUser($user_id, $param);

        return $result ? json(['code' => 1, 'msg' => '修改成功!']) : json(['code' => 0, 'msg' => '修改失败!']);
    }

    /**
     * 上传接口
     * */
    public function upload()
    {
        $file = $this->request->file('file');

        if (!$file->isFile()) {
            return json(['code' => 0, 'msg' => '无效的文件!']);
        }

        $savename = Filesystem::disk('public')->putFile('api', $file);

        return json(['code' => 1, 'src' => $this->request->domain() . '/storage/' . $savename, 'msg' => '上传成功!']);
    }

    /**
     * 用户统计
     * */
    public function total()
    {
        $user_id = $this->request->user['user_id'];
        $data = UserService::getUserTotalInfo($user_id);
        $invite_code = UserService::getPromotionCode($user_id);
        $data['user_info']['invite_code'] = $invite_code;
        // if (!empty($data['user_info'])) {
        //     $data['user_info']['is_agent'] = $is_agent;
        //     $data['user_info']['type'] = $type;
        //     $data['user_info']['invite_code'] = $invite_code;
        //     // $data['user_info']['shop_promotion_code'] = $shop_promotion_code;
        // }
        return json(['code' => 1, 'data' => $data, 'msg' => '请求成功!']);
    }

    /**
     * 余额明细
     * */

    public function balance()
    {

        $user_id = $this->request->user['user_id'];
        $data = UserService::getBalanceDetail($user_id);

        return json(['code' => 1, 'data' => $data, 'msg' => '请求成功!']);
    }

    /**
     * 积分明细
     * */

    public function integral()
    {
        $user_id = $this->request->user['user_id'];
        $data = UserService::getIntegralDetail($user_id);

        return json(['code' => 1, 'data' => $data, 'msg' => '请求成功!']);
    }

    /**
     * 金豆-明细
     * */

    public function gold()
    {
        $user_id = $this->request->user['user_id'];
        $data = UserService::getGoldDetail($user_id);

        return json(['code' => 1, 'data' => $data, 'msg' => '请求成功!']);
    }

}
